package lang.enumeration.safe;

// see Calculator.Operator
public class SafeEnumType {
	public static void main(String[] args) {
		double td = Calculator.calculate(1d, Calculator.Operation.TIMES, 2.2d);
		System.out.println(td);
	}
}

class Calculator {
	
	public static double calculate(double x, Operation op, double y) {
		return op.eval(x, y);
	}

	// 为每个实例提供单独的子类）
	public static abstract class Operation{
		private final String name;
		
		private Operation(String name) {
			this.name = name;
		}
		
		public String toString() {
			return this.name;
		}
		
		// Perform artithmetic op represented by this constant
		abstract double eval(double x, double y);
		
		// Doubly nested anonymous classes
		public static final Operation PLUS = new Operation("+") {
			double eval(double x, double y) {
				return x + y ;
			}
		};
		
		public static final Operation MINUS = new Operation("-") {
			double eval(double x, double y) {
				return x - y ;
			}
		};
		
		public static final Operation TIMES = new Operation("*") {
			double eval(double x, double y) {
				return x * y;
			}
		};
		
		public static final Operation DEVIDE = new Operation("/") {
			double eval(double x, double y) {
				return x / y;
			}
		};
	}
}
